package com.dozen.commonbase.view.banner.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import com.dozen.commonbase.R;

/**
 * 描述 Banner的图片指示器控件。
 */

public class PictureIndicatorView extends BannerIndicator {

    private int normalPictureNow = R.mipmap.me_banner_un_selected;
    private int normalPictureColor = 0;
    private int selectPictureNow = R.mipmap.me_banner_selected;
    private int selectPictureColor = 0;
    private boolean isCenterNow = true;
    private int leftWidthSize = 0;
    private int itemWidth = 50;

    public PictureIndicatorView(Context context) {
        this(context, null);
    }

    public PictureIndicatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("Recycle")
    public PictureIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = attrs == null ? null : context.obtainStyledAttributes(attrs, R.styleable.PictureIndicatorView);
        if (ta != null) {
            normalPictureNow = ta.getResourceId(R.styleable.PictureIndicatorView_normalPictureNow, normalPictureNow);
            normalPictureColor = ta.getColor(R.styleable.PictureIndicatorView_normalPictureColor, normalPictureColor);
            selectPictureNow = ta.getResourceId(R.styleable.PictureIndicatorView_selectPictureNow, selectPictureNow);
            selectPictureColor = ta.getColor(R.styleable.PictureIndicatorView_selectPictureColor, selectPictureColor);
            isCenterNow = ta.getBoolean(R.styleable.PictureIndicatorView_isCenterNow, isCenterNow);
            leftWidthSize = ta.getInteger(R.styleable.PictureIndicatorView_leftWidthSize, leftWidthSize);
            itemWidth = ta.getInteger(R.styleable.PictureIndicatorView_itemWidth, itemWidth);
            ta.recycle();
        }

    }

    @Override
    protected int onMeasureWidth() {

        return 0;
    }

    @Override
    protected int onMeasureHeight() {
        //最大的点的直径 = 总高度。
        return 0;
    }

    @SuppressLint({"RtlHardcoded", "DrawAllocation"})
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int curPosition = getCurPosition();
        int width = getWidth();
        int startX = leftWidthSize;
        if (isCenterNow) {
            startX += width / 2 - itemWidth * getTotalCount() / 2;
        }

        Paint paint = getPaint();
        for (int i = 0; i < getTotalCount(); i++) {
            Bitmap bitmap;
            int selectColor;
            if (i == curPosition) {
                bitmap = BitmapFactory.decodeResource(getResources(), selectPictureNow);
                selectColor = selectPictureColor;
            } else {
                bitmap = BitmapFactory.decodeResource(getResources(), normalPictureNow);
                selectColor = normalPictureColor;
            }
            if (selectColor != 0) {
                paint.setColorFilter(new PorterDuffColorFilter(selectColor, PorterDuff.Mode.SRC_IN));
            }
            Matrix matrix = new Matrix();
            float location = bitmap.getWidth();
            matrix.setTranslate(startX - location / 2, 0);
            canvas.drawBitmap(bitmap, matrix, paint);
            startX += itemWidth;
        }
    }
}
